package com.nowcoder.code.lcof;

import com.algorithm.model.TreeLinkNode;

public class JZ8 {

    public static void main(String[] args) {

    }

    TreeLinkNode ans;
    TreeLinkNode last;

    public TreeLinkNode GetNext(TreeLinkNode pNode) {
        ans = pNode;
        TreeLinkNode root = pNode;
        while (root.next != null) {
            root = root.next;
        }
        return inOrder(root);
    }


    public TreeLinkNode inOrder(TreeLinkNode node) {

        if (node == null) {
            return null;
        }

        TreeLinkNode treeLinkNode = inOrder(node.left);
        if (treeLinkNode != null) {
            return treeLinkNode;
        }
        if (last == ans) {
            return node;
        }
        last = node;
        return inOrder(node.right);
    }

}
